<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class DemoController extends Controller
{
    public function users(Request $request)
    {
      if ($request->get('code')) {
        return response()->json($this->usersData(), $request->get('code'));
      }
      return response()->json($this->usersData());
    }

    public function userDelete(Request $request, $uid)
    {
      $users = $this->usersData();
      unset($users[$uid]);
      if ($request->get('code')) {
        return response()->json($users, $request->get('code'));
      }
      return response()->json($users);
    }

    public function userUpdate(Request $request, $uid)
    {
      $users = $this->usersData();
      $users[$uid]['name'] = $request->get('name');
      if ($request->get('code')) {
        return response()->json($users, $request->get('code'));
      }
      return response()->json($users);
    }

    public function userNew(Request $request)
    {
      $users = $this->usersData();
      $uid = count($users) + 1;
      $users[$uid] = array(
        'id' => $uid,
        'name' => '张' . $uid
      );
      if ($request->get('code')) {
        return response()->json($users, $request->get('code'));
      }
      return response()->json($users);
    }

    public function usersData()
    {
      return [
          1 => [
            'id' => 1,
            'name' => '张一'
          ],
          2 => [
              'id' => 2,
              'name' => '张2'
          ],
          3 => [
              'id' => 3,
              'name' => '张3'
          ],
          4 => [
              'id' => 4,
              'name' => '张4'
          ],
          5 => [
              'id' => 5,
              'name' => '张4'
          ]
      ];
    }
}
